<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="currentPageData"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="softId"
        header-align="center"
        align="center"
        width="145"
        label="数据条ID">
      </el-table-column>
      <el-table-column
        prop="softName"
        header-align="center"
        align="center"
        label="软件名字">
      </el-table-column>
      <el-table-column
        prop="columnName"
        header-align="center"
        align="center"
        label="所属分组">
      </el-table-column>
      <el-table-column
        prop="softLogo"
        header-align="center"
        align="center"
        width="130"
        label="软件logo">
        <template slot-scope="scope">
          <img :src="scope.row.softLogo" style="width: 100px; height: 80px"/>
        </template>
      </el-table-column>
      <el-table-column
        prop="edition"
        header-align="center"
        align="center"
        label="版本信息">
      </el-table-column>
      <el-table-column
        prop="language"
        header-align="center"
        align="center"
        label="软件语言">
      </el-table-column>
      <el-table-column
        prop="platform"
        header-align="center"
        align="center"
        label="支持平台">
      </el-table-column>
      <el-table-column
        prop="softSize"
        header-align="center"
        align="center"
        label="软件大小">
      </el-table-column>
      <el-table-column
        prop="softAddr"
        header-align="center"
        align="center"
        label="下载地址">
        <template slot-scope="scope">
          <a :href="scope.row.softAddr">点击下载</a>
        </template>
      </el-table-column>
      <el-table-column
        prop="userId"
        header-align="center"
        align="center"
        label="发布者ID">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="100"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        width="100"
        label="更新时间">
      </el-table-column>
      <el-table-column
        prop="verify"
        header-align="center"
        align="center"
        width="150"
        label="审核状态">
        <template slot-scope="scope">
          <el-select v-model="scope.row.verify" @change="updateSoftStatus(scope.row)">
            <el-option
              v-for="(item) in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        prop="downloads"
        header-align="center"
        align="center"
        label="下载次数">
      </el-table-column>
<!--      <el-table-column-->
<!--        prop="installProleft"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="">-->
<!--      </el-table-column>-->
      <el-table-column
        prop="showPic"
        header-align="center"
        align="center"
        width="150"
        label="界面展示">
        <template slot-scope="scope">
          <img :src="scope.row.showPic" style="width: 100px; height: 80px"/>
        </template>
      </el-table-column>
<!--      <el-table-column-->
<!--        prop="installProright"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="">-->
<!--      </el-table-column>-->
      <el-table-column
        prop="briefIntro"
        header-align="center"
        align="center"
        label="简短介绍">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="gotoDetail_soft(scope.row.softId)">详情/修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.softId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <span>共{{dataList.length}}条数据</span>
      <button @click="prevPage()" class="b">
        上一页
      </button>
      <span>第{{currentPage}}页/共{{totalPage}}页</span>
      <button @click="nextPage()">
        下一页
      </button>
      前往第<input type="text" id="go" @change="toPage" style="width:20px;text-align: center">页
    </div>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataVerifyList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './software-add-or-update'
  export default {
    data () {
      return {
          options: [{
              value: 0,
              label: '暂不审核'
          }, {
              value: 1,
              label: '通过'
          }, {
              value: 2,
              label: '不通过'
          }],
        dataForm: {
          key: ''
        },
        dataList: [],
        currentPageData:[],
        totalPage: 1, // 统共页数，默认为1
        currentPage: 1, //当前页数 ，默认为1
        pageSize: 8, // 每页显示数量
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataVerifyList()
    },
    methods: {
      //获取/修改软件详细信息
      gotoDetail_soft(row){
        this.$router.replace({
          path:'/verify-softwareInfo',
          query:{softId:row}
        })
      },
        updateSoftStatus(data){
            let{softId,verify}=data;
            this.$http({
                url: this.$http.adornUrl('/generator/software/update'),
                method:'post',
                data:this.$http.adornData({softId,verify},false)
            }).then(({data}) => {
                this.$message({
                    type: "success",
                    message:"状态更新成功",
                })
              this.getDataVerifyList()
            })
        },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/generator/software/list'),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
          } else {
            this.dataList = []
          }
          this.dataListLoading = false
        })
      },
        getDataVerifyList () {
            this.dataListLoading = true
            this.$http({
                url: this.$http.adornUrl('/generator/software/verify/list'),
                method: 'get'
            }).then(({data}) => {
                if (data && data.code === 0) {
                    this.dataList = data.data
                  this.page()

                } else {
                    this.dataList = []
                }
                this.dataListLoading = false
            })
        },

      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.softId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/generator/software/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      //分页
      page(){
        this.totalPage = Math.ceil(this.dataList.length / this.pageSize);
        // 计算得0时设置为1
        this.totalPage = (this.totalPage = 0 ? 1 : this.totalPage);
        this.setCurrentPageData();
      },
      // 设置当前页面数据
      setCurrentPageData() {
        let begin = (this.currentPage - 1) * this.pageSize;
        let end = this.currentPage * this.pageSize;
        this.currentPageData = this.dataList.slice(begin, end);//当前页软件显示的内容
        console.log(this.currentPageData);
      },
      //上一页 软件
      prevPage() {
        if (this.currentPage === 1) return;
        this.currentPage--;
        this.setCurrentPageData();
      },
      // 下一页
      nextPage() {
        if (this.currentPage === this.totalPage) return;
        this.currentPage++;
        this.setCurrentPageData();
      },
      //跳转到指定页数
      toPage() {
        this.currentPage= document.getElementById("go").value;
        if(this.currentPage>this.totalPage){
          this.$message.error("超过总页数，请重新输入！");
        }
        this.setCurrentPageData();
      }
    }
  }
</script>

<style>
  .page{
    transform: translateX(80%);
    margin-top: 20px;
  }
  .page button,.page input{
    border: 1px solid #333;
    padding: 4px;
    border-radius: 4px;
  }

</style>
